﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demos.DesignPatterns._09_组合模式
{
    /// <summary>
    /// 
    /// </summary>
    public class ProductCategory : IProductComponent
    {
        /// <summary>
        /// 存储字典
        /// </summary>
        private readonly List<IProductComponent> _children = new List<IProductComponent>();

        public string Name { get; }

        public ProductCategory(string name) => Name = name;

        public void Add(IProductComponent component) => _children.Add(component);

        public void Remove(IProductComponent component) => _children.Remove(component);

        public void Display(int depth = 0)
        {
            Console.WriteLine($"{new string('-', depth * 2)} 分类：{Name}");
            foreach (var child in _children)
            {
                child.Display(depth + 1);
            }
        }

        public decimal GetPrice() => _children.Sum(c => c.GetPrice());
    }
}
